/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "几何插座.h"
#include "通用插座.h"

#include "C_物体插座.h"
#include "list/list物体插座.h"
#include "节点/编译声明代码.h"





C_多边形边插座::C_多边形边插座(std::wstring name) :C_插座基类(name, E_值类型::e_Type_多边形边) {
	m_默认值.polygon = nullptr;
	m_Value = &m_默认值;
}

C_多边形边插座::~C_多边形边插座() {
}

void* C_多边形边插座::f_getData(uint8 当前插座连接线ID) {
	C_插座基类* socket = f_getLinkOutSocket(当前插座连接线ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_多边形边) {
			return socket->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_多边形边1D) {
			auto& data = *DEF_多边形边_1D插座数据(socket, 0);
			if (data.size()) {
				//m_Value = data[0];
			}
		}
		else if (socket->m_Type == E_值类型::e_插座Type_引用数据) {
			auto& inData = *DEF_引用插座数据(socket, 当前插座连接线ID);
			if (inData.m_Type == E_值类型::e_Type_多边形) {
				return inData.m_Data;
			}
		}
	}
	else if (f_isLink() == false) {
		m_Value = &m_默认值;
	}
	return m_Value;
}

void C_多边形边插座::f_setData(void* data, uint8 线程ID) {
	m_Value = ((S_边*)data);
}

std::string C_多边形边插座::f_构建变量代码(int32 loc) {
	C_插座基类* socket = f_getLinkOutSocket(loc);
	std::string code;
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_多边形边) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "_new_val_to_Edge";

		code = "S_边* " + m_Code;
		switch (socket->m_Type) {
			case E_值类型::e_Type_Vec3_Array1D: {
				//code += " = f_Edge1D_array_fromEdge_alloc(f_多边形_创建边(" + socket->f_getCode(loc) + ",  false));\n";
				break;
			case E_值类型::e_Type_多边形边1D: {
				code += " = " + socket->f_getCode(loc) + "->ptr_userData[0];\n";
				break;
			}
			default:
				return code;
			}
		}

		//m_pNode->m_ReleaseCode += f_代码构建_销毁数组(m_Code);
	}
	else {
		C_插座基类::f_构建变量代码(loc);
		m_Code = "(&(" + m_Code + "))";
	}

	return code;
}

bool C_多边形边插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_Array:
		case E_值类型::e_Type_Vec3_1D:
		case E_值类型::e_Type_Vec3_Array1D:
		//case E_值类型::e_Type_uVec2:
		case E_值类型::e_Type_多边形边:
		case E_值类型::e_Type_多边形边1D:
			return true;
	}
	return false;
}






C_多边形边_1D插座::C_多边形边_1D插座(std::wstring name) :C_插座基类(name, E_值类型::e_Type_多边形边1D) {
	m_默认值 = (S_边Array*)f_core_array_new(0, sizeof(S_边*));
	m_转换值 = (S_边Array*)f_core_array_new(0, sizeof(S_边*));
	m_Value = m_默认值;
}

C_多边形边_1D插座::~C_多边形边_1D插座() {
	f_core_array_free((S_Array*)m_默认值);
	f_core_array_free((S_Array*)m_转换值);
}

void* C_多边形边_1D插座::f_getData(uint8 当前插座连接线ID) {
	C_插座基类* socket = f_getLinkOutSocket(当前插座连接线ID);
	if (socket) {
		switch (socket->m_Type) {
			case  E_值类型::e_Type_Array:
			case E_值类型::e_Type_多边形边1D: return socket->f_getData(0);

			case E_值类型::e_Type_多边形边: {
				auto v = DEF_多边形边插座数据(socket, 0);
				f_core_array_resize((S_Array*)m_转换值, 1);
				m_转换值->ptr_userData[0] = v;
				break;
			}

			default:
				f_core_array_resize((S_Array*)m_转换值, 0);
		}

		m_Value = m_转换值;
	}
	else if (!m_映射指针) {
		m_Value = m_默认值;
	}
	return m_Value;
}

void C_多边形边_1D插座::f_setData(void* data, uint8 线程ID) {
	if (data) {
		m_Value = (S_边Array*)data;
		m_映射指针 = true;
	}
	else {
		m_Value = (S_边Array*)m_默认值;
		m_映射指针 = false;
	}
}

std::string C_多边形边_1D插座::f_构建变量代码(int32 loc) {
	C_插座基类* socket = f_getLinkOutSocket(loc);
	std::string code;
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_多边形边1D) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "_new_val_to_Edge_Array1D";

		code += "S_边Array* " + m_Code + " = " + f_代码构建_创建数组(E_值类型::e_Type_多边形边1D, 1);
		switch (socket->m_Type) {
			case E_值类型::e_Type_多边形边: {
				code += m_Code + "->ptr_userData[0] = " + socket->f_getCode(0) + ";\n";
				break;
			default:
				return code;
			}
		}
		//m_pNode->m_Code += code;
		m_pNode->m_ReleaseCode += f_代码构建_销毁数组(m_Code);
	}
	else {
		C_插座基类::f_构建变量代码(loc);
		m_Code = "(&" + m_Code + ")";
	}

	return code;
}

bool C_多边形边_1D插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_Array:
		case E_值类型::e_Type_多边形边:
		case E_值类型::e_Type_多边形边1D:
			return true;
	}
	return false;
}



C_多边形面插座::C_多边形面插座(std::wstring name) :C_插座基类(name, E_值类型::e_Type_多边形面) {
	m_默认值 = new S_面;
	m_转换值 = new S_面;

	m_默认值->polygon = nullptr;
	m_转换值->polygon = nullptr;

	m_Value = m_默认值;
}

C_多边形面插座::~C_多边形面插座() {
	delete m_默认值;
	delete m_转换值;
	C_插座基类::~C_插座基类();
}

void* C_多边形面插座::f_getData(uint8 当前插座连接线ID) {
	C_插座基类* socket = f_getLinkOutSocket(当前插座连接线ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_多边形面) {
			return socket->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_多边形面1D) {
			
		}
		else if (socket->m_Type == E_值类型::e_Type_Object_Array1D) {

		}
	}

	return m_Value;
}

void C_多边形面插座::f_setData(void* data, uint8 线程ID) {
	m_Value = (S_面*)data;
}

std::string C_多边形面插座::f_构建变量代码(int32 loc) {
	std::string code;
	C_插座基类* socket = f_getLinkOutSocket(loc);

	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_多边形面) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "_to_Element";
		code = DEF_S(S_面*)" " + m_Code + ";\n";
		switch (socket->m_Type) {
		case E_值类型::e_Type_多边形元素: {
			code += m_Code + " = " + DEF_S(f_surface_取多边形面)"(g多边形, " + socket->f_getCode(loc) + ", 0);\n";
			break;
		}
		default:
			break;
		}
	}
	else {
		C_插座基类::f_构建变量代码(loc);
		m_Code = "&" + m_Code;
	}
	return code;
}





C_多边形元素插座::C_多边形元素插座(std::wstring name) :C_插座基类(name, E_值类型::e_Type_多边形元素) {
	m_默认值 = (S_多边形元素*)f_core_array_new(0, sizeof(S_面*));
	m_转换值 = (S_多边形元素*)f_core_array_new(0, sizeof(S_面*));
	m_Value = m_默认值;
}

C_多边形元素插座::~C_多边形元素插座(){
	f_core_array_free((S_Array*)m_默认值);
	f_core_array_free((S_Array*)m_转换值);
}

void* C_多边形元素插座::f_getData(uint8 当前插座连接线ID) {
	C_插座基类* socket = f_getLinkOutSocket(当前插座连接线ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_多边形元素) {
			return socket->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_多边形元素1D) {
			auto& face = *DEF_多边形面_1D插座数据(socket, 0);
			if (face.size()) {
				return face[0];
			}
			else {
				//m_转换值.面索引.clear();
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_Object) {
			auto* ob = DEF_物体插座数据(socket, 0);
			if (ob->m_Type == E_物体类型::t_多边形) {
				((S_多边形*)ob->m_UserData)->m_元素组;
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_Object_Array1D) {

		}
		//m_Value = &m_转换值;
	}
	else {
		//m_Value = &m_默认值;
	}

	return m_Value;
}

void C_多边形元素插座::f_setData(void* data, uint8 线程ID) {
	m_Value = (S_多边形元素*)data;
}

std::string C_多边形元素插座::f_构建变量代码(int32 loc) {
	std::string code;
	C_插座基类* socket = f_getLinkOutSocket(loc);

	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_多边形元素) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "_to_Element";
		
		switch (socket->m_Type) {
			case E_值类型::e_Type_多边形元素: {
				m_Code  = socket->f_getCode(loc) + "->ptr_userData[0];\n";
				return "";
			}
		}

		code += DEF_S(S_多边形元素*)" " + m_Code + " = " + f_代码构建_创建Array(E_值类型::e_Type_多边形元素, "1");
		switch (socket->m_Type) {
			case E_值类型::e_Type_多边形面: {
				break;
			}
			case E_值类型::e_Type_多边形: {
				socket->f_getCode(loc);
				break;
			}
			default:
				break;
		}

		m_pNode->m_ReleaseCode += f_代码构建_销毁数组(m_Code);
	}
	else {
		C_插座基类::f_构建变量代码(loc);
		m_Code = "&" + m_Code;
	}
	return code;
}

bool C_多边形元素插座::f_接入插座是否匹配(E_值类型 type) {
	switch (m_Type) {
	//case E_值类型::e_Type_多边形面: 
	case E_值类型::e_Type_多边形元素: 
		return true;
	}
	return false;
}






C_多边形元素_1D插座::C_多边形元素_1D插座(std::wstring name) :C_插座基类(name, E_值类型::e_Type_多边形元素1D) {
	m_默认值 = (S_多边形元素Array*)f_core_array_new(0, sizeof(S_多边形元素*), true);
	m_转换值 = (S_多边形元素Array*)f_core_array_new(0, sizeof(S_多边形元素*), true);
	m_Value = m_默认值;

	m_默认值->ptr_userData[0] = nullptr;
	m_转换值->ptr_userData[0] = nullptr;
}

C_多边形元素_1D插座::~C_多边形元素_1D插座(){
	f_core_array_free((S_Array*)m_默认值);
	f_core_array_free((S_Array*)m_转换值);
	C_插座基类::~C_插座基类();
}

void* C_多边形元素_1D插座::f_getData(uint8 当前插座连接线ID) {
	C_插座基类* socket = f_getLinkOutSocket(当前插座连接线ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_多边形元素1D) {
			return socket->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_多边形元素) {
			f_core_array_resize((S_Array*)m_转换值, 0);
			m_转换值->ptr_userData[0] = DEF_多边形元素插座数据(socket, 0);
		}
		else if (socket->m_Type == E_值类型::e_Type_Object) {
			auto* ob = DEF_物体插座数据(socket, 0);
			if (ob->m_Type == E_物体类型::t_多边形) {
				m_Value = ((S_多边形*)ob->m_UserData)->m_元素组;
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_Object_1D) {
			auto* ob = DEF_物体插座_1D数据(socket, 0);
			if (ob->count && ob->ptr_userData[0]->m_Type == E_物体类型::t_多边形) {
				m_Value = ((S_多边形*)ob->ptr_userData[0]->m_UserData)->m_元素组;
			}
		}
	}
	else {
		m_Value = m_默认值;
	}
	return m_Value;
}

void C_多边形元素_1D插座::f_setData(void* data, uint8 线程ID) {
	m_Value = (S_多边形元素Array*)data;
}

std::string C_多边形元素_1D插座::f_构建变量代码(int32 loc) {
	std::string code;
	C_插座基类* socket = f_getLinkOutSocket(loc);

	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_多边形元素1D) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "_to_Element1D";

		code += DEF_S(S_多边形元素Array*)" " + m_Code + " = " + f_代码构建_创建Array(E_值类型::e_Type_多边形元素1D, "1");
		//code = DEF_S(f_core_array_resize)"((S_Array*)" + m_Code + ", 1);\n";
		switch (socket->m_Type) {
		case E_值类型::e_Type_多边形面: {
			code += m_Code + "->ptr_userData[0] = " + socket->f_getCode(loc) + ";\n";
			break;
		}
		case E_值类型::e_Type_多边形元素: {
			code += m_Code + "->ptr_userData[0] = " + socket->f_getCode(loc) + ";\n";
			break;
		}
		}
		
		m_pNode->m_ReleaseCode += f_代码构建_销毁数组(m_Code);
	}
	else {
		C_插座基类::f_构建变量代码(loc);
		m_Code = "&" + m_Code;
	}
	return code;
}

bool C_多边形元素_1D插座::f_接入插座是否匹配(E_值类型 type) {
	switch (m_Type) {
	case E_值类型::e_Type_多边形元素1D: 
	case E_值类型::e_Type_多边形元素: 
		return true;
	}
	return false;
}





